<?php 
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();

$type = isset($_GET['type'])?$_GET['type']:1;
if(!empty($_GET['act'])){
	$act = $_GET['act'];
	if($act == 'del'){
		$db->sql = "DELETE FROM coupon WHERE id = ".$_GET['id'];
		$db->doExecute();
	}
	if($act == "edit"){
		if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
			$couponName = $_POST['couponName'];
			$couponUrl  = $_POST['couponUrl'];
			$couponImg  = $_POST['couponImg'];
			$couponID   = $_POST['couponID'];
			$couponType = $_POST['couponType'];
			
			if($couponID){
			    $db->sql = "UPDATE coupon SET title = '{$couponName}',pic = '{$couponImg}' ,url = '{$couponUrl}' WHERE id = ".$couponID;
			}else{
			    $db->sql = "insert into coupon( pic, title, url, type)  values('{$couponImg}','{$couponName}','{$couponUrl}',{$couponType}) ";
			}
			$db->doExecute();
		}
	}
}

$db->sql = "SELECT * FROM coupon WHERE type = {$type} ORDER BY id DESC";
$coupons = $db->doResultList2();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />

<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	$("form").submit(function(){
		if( !$("input[name='userName']").val() || !$("input[name='newPwd']").val() || !$("input[name='againPwd']").val() || !$("input[name='nickName']").val() ){
			alert("都为必填项，不能为空！");
			return false;
		}
	});
	//删除
	$(".deleteUser").click(function(){
		if(!confirm("是否将此用户删除?")){
			return false;
		}
	});
	//修改
	$(".editcoupon").click(function(){
		var couponName = $(this).parents("tr").children("td").eq(2).text();
		var couponID = $(this).parents("tr").children("td").eq(0).text();
		var couponUrl = $(this).parents("tr").children("td").eq(3).text();
		var couponImg = $(this).parents("tr").children("td").eq(1).find("img").attr("src");
		//var couponCost = $(this).parents("tr").children("td").eq(4).text();
		$("#couponName").val(couponName);
		$("#couponUrl").val(couponUrl);
		$("#couponImg").val(couponImg);
		$("#couponID").val(couponID);

		//var activity_id = tiqu(couponUrl ,/activity_id=([0-9]+)($|&)/i);
		//var seller_id = tiqu(couponUrl ,/seller_id=([0-9]+)($|&)/i);

		//alert(activity_id+'~~'+seller_id);
		$("#message_click").click();
	});
	$(".status").click(function(){
		//var status = $(this).children('span').text();
		var id = $(this).parents("tr").children("td").eq(0).text();
		var this1 = $(this);
		//alert(id);
		$.ajax({
			type: "GET",
			url: "userStatusChange.php",
			data: "id="+id+"&",
			dataType: "html",
			success: function(date){
				//alert(this1.text());
				//alert($(this).text());
				if(date==1){
					//alert(date);
					this1.html('<span style="color:black;">启用</span>');
				}else{
					this1.html('<span style="color:gray;">禁用</span>');
				}
			}
		});
	});
	$(".success").ajaxSuccess(function(evt, request, settings){
		$(this).show();
	});
	$(".error").ajaxError(function(event,request, settings){
		$(this).show();
	});
});
function tiqu(s,re){
	//var s = "http://taoquan.taobao.com/coupon/combo/between.htm?spm=9210.1000627.1.2.EyUN0E&activityId=26448135&sellerId=383261993&shopTitle=%B8%BB%B0%D8%BE%C5%B0%D8%D7%A8%C2%F4%B5%EA&discount=5000";  
	//var re = /activityId=([0-9]+)($|&)/i; // 创建正则表达式模式。  
	var r = s.match(re); // 尝试匹配搜索字符串。  
	return (r[1]); // 返回第一次出现 "ain" 的地方。 
}
</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">
$(function($) {
	$(".group-pic").colorbox({rel:'group-pic',width:"30%", height:"50%"});
	$(".couponStatusChange").click(function(){
		var _this = $(this);
		var str = '';
	    if(_this.text()=="使用中"){
		    str = "禁用中";
		    _this.css("color","gray");  //#57a000
		}
	    if(_this.text()=="禁用中"){ 
		    str = "使用中";
		    _this.css("color","#57a000");
		}
	    _this.text(str);
	    //修改状态
	    $.ajax({
	    	  type: "GET",
	    	  url: "youpinAjax.php?act=couponstatuschange",
	    	  data: "id="+_this.data("id"),
	    	  dataType: "html",
	    	  success: function(){
	    		    
	    	  }
	   });
	    
	});
});
</script>
</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>简短描述输入及修改</strong>
			</p>
			<form action="?act=edit&type=<?php echo $type;?>" method="post">
				<fieldset>
					标题：<input class="text-input " name="couponName" id="couponName" type="text" value="" style="width: 310px;"/>
					链接：<input class="text-input " name="couponUrl" id="couponUrl" type="text" value="" style="width: 310px;"/>
					图片：<input class="text-input " name="couponImg" id="couponImg" type="text" value="" style="width: 310px;"/>
					<input class="text-input " id="couponID" name="couponID" type="hidden" value="" style="width: 310px;"/>
					<input class="text-input " id="couponType" name="couponType" type="hidden" value="<?php echo $type;?>" style="width: 310px;"/>
					<input class="button" type="submit" value="修改" /> 
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide().siblings('#facebox_overlay').hide();">关闭</a>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">优惠券管理规则：优惠券数量必须大于等于6个。优惠券大于6个时，显示编号最大的6个</p>

			<div class="clear"></div>
			<!-- End .clear -->

			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-header">

					<h3>优惠券管理&nbsp;&nbsp;&nbsp;&nbsp;>&nbsp;&nbsp;&nbsp;&nbsp;<?php if($type==1) echo "密码券";if($type==2) echo "普通券";?></h3>

					<div class="clear"></div>

				</div>
				<!-- End .content-box-header -->

				<div class="content-box-content">

				<!-- Start Notifications -->
				
				<div class="notification success png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						修改成功
					</div>
				</div>
				
				<div class="notification error png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					修改失败
					</div>
				</div>
				
				<!-- End Notifications -->
				
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->
                        &nbsp;&nbsp;<h4 style="margin-left: 10px;"><?php if($type==1):echo "密码券";else:?><a href="?type=1">密码券</a><?php endif;?><span style="margin-left: 30px;"><?php if($type==2):echo "普通券";else:?><a href="?type=2" >普通券</a><?php endif;?></span></h4>
						&nbsp;&nbsp;<h4 style="margin-left: 10px;"><a href="javascript:void(0)" onclick='$("#couponName").val("");$("#couponUrl").val("");$("#couponImg").val("");$("#couponID").val("");$("#message_click").click();return false;'>添加</a></h4>
						<table>
							<thead>
								<tr>
									<th>编号</th>
									<th>图片</th>
									<th>标题</th>
									<th style="width: 50%">地址</th>
									<th>操作</th>
								</tr>

							</thead>

							<tbody>
							<?php if(count($coupons)>0):?>
							<?php foreach ($coupons as $key=>$item):?>
								<tr>
									<td><?php echo $item['id'];?></td>
									<td>
										<p><a class="group-pic" href="<?php echo $item['pic'];?>" title="<?php echo $item['title'];?>">
											<img src="<?php echo $item['pic'];?>" style="width: 50px;height: 50px;"/>
										</a></p>
									</td>
									<td><?php echo $item['title'];?></td>
									<td><?php echo $item['url'];?></td>
									
									<td>
										
										<a class="editcoupon" href="javascript:void(0);">修改</a>
										<a class="deleteUser" href="?id=<?php echo $item['id'];?>&act=del&type=<?php echo $type;?>">删除</a>
										<br /><br />
										<a class="couponStatusChange" data-id="<?php echo $item['id'];?>"><?php if($item['status']==1) echo "使用中"; if($item['status']==0) echo "禁用中";?></a>
										
									</td>
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="5" style="text-align: center;">没有优惠券！！</td></tr>
							<?php endif;?>
							</tbody>

						</table>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
